package com.aiwei.tdjk.utils;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by HHB on 6/2 0002.
 * 缓存工具类
 */
public class EHCacheUtils {

//    @Autowired
    private   CacheManager cacheManager;

    public CacheManager getCacheManager() {
        return cacheManager;
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    private  Cache cache = cacheManager.getCache("authorization");
//    public EHCache(){
//            this.cache = cacheManager.getCache("authorization");
//    }
    public Cache getCache() {
        return cache;
    }

    public void setCache(Cache cache) {
        this.cache = cache;
    }



    /*
 * 通过名称从缓存中获取数据
 */
    public  Object  getCacheElement(String cacheKey) throws Exception {
        net.sf.ehcache.Element e = cache.get(cacheKey);
        if (e == null) {
            return null;
        }
        return e.getValue();
    }
    /*
     * 将对象添加到缓存中
     */
    public  void addToCache(String cacheKey, Object result) throws Exception {
        Element element = new Element(cacheKey, result);
        cache.put(element);
    }
}
